/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import dnbW.dbW.DatabaseInfo;
import dnbW.uiW.dbW.DatabaseController;
import dnbW.uiW.queryW.visual.IdentityPathSQLTask;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Statement;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import tree.TreeBean;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeNode;
import java.util.HashMap;
import java.util.Map;
import java.util.Enumeration;
/**
 *
 * @author Andree
 */

public class ValueTree extends HttpServlet {
    
    private net.minidev.json.JSONArray data;
    
    private TreeBean tb;
    
    public List nodes = null;
    
    public List<String> detailPath = new ArrayList<String>();
    
    int finalResultCount = 0;
    
    private long levelCount = 0;
    
    public static HashMap<String, DefaultMutableTreeNode> currentSelectedNodes = null;
    
    public static Map valueTreeMap = new HashMap();
    
    DefaultMutableTreeNode tabSwitchedTree;

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        String root = request.getParameter("root");
        
        try{
                        
                        if(root.equals("source"))
			{          
                                       currentSelectedNodes = new HashMap<String, DefaultMutableTreeNode>();
                                       
                                       if(!Controller.isTabSwitchedForValueTree)
                                       {
                                           data = new net.minidev.json.JSONArray();
                                           
                                           data = getFinalTree(Controller.finalTree);
                                           
                                           valueTreeMap.put(Controller.tabNumber, Controller.finalTree);
                                       }   
                                       else if(Controller.isTabSwitchedForValueTree)
                                       {
                                           data = new net.minidev.json.JSONArray();
                                           
                                           tabSwitchedTree = new DefaultMutableTreeNode();
                                           Object O = valueTreeMap.get(Controller.tabNumber);
                                           tabSwitchedTree = (DefaultMutableTreeNode)O;
                                           
                                           if(tabSwitchedTree != null)
                                                data = getFinalTree(tabSwitchedTree);
                                       }                                                                            
                        }
                          else if(root.contains("results"))
      			{
                            data = new net.minidev.json.JSONArray();
                            DefaultMutableTreeNode node = null;
                            
                            if(!Controller.isTabSwitchedForValueTree)
                                node = (DefaultMutableTreeNode)Controller.finalTree.getFirstChild();
                            else if(Controller.isTabSwitchedForValueTree)
                                node = (DefaultMutableTreeNode)tabSwitchedTree.getFirstChild();
                            
                            String idValue = "Level_"+(++levelCount);
                            currentSelectedNodes.put(idValue, node);
                               tb = new TreeBean();
                               tb.setId(idValue);
                               tb.setText(node.toString());
                               tb.setClasses(node.toString());
                               
                               if(node.getChildCount()>0)
                                   tb.setHasChildren(true);
                               else
                                   tb.setHasChildren(false);
				   
                               tb.setExpanded(false);

                               data.add(tb);
                            
                        }
                        else
                          {
                              data = new net.minidev.json.JSONArray();
                              
                              DefaultMutableTreeNode node = currentSelectedNodes.get(root);
                              
                              for (Enumeration e = node.children(); e.hasMoreElements();) {
                                    DefaultMutableTreeNode childNode = (DefaultMutableTreeNode) (e.nextElement());
                                    String idValue = "Level_"+(++levelCount);
                                    currentSelectedNodes.put(idValue, childNode);
                                    tb = new TreeBean();
                                    tb.setId(idValue);
                                    tb.setText(childNode.toString());
                                    tb.setClasses(childNode.toString());
                                    
                                    if(childNode.getChildCount()>0)
                                       tb.setHasChildren(true);
                                    else
                                       tb.setHasChildren(false);

                                    tb.setExpanded(false);

                                    data.add(tb);
                                }
                          }
                        response.setContentType("application/json");
                        response.setCharacterEncoding("UTF-8");
                        response.getWriter().print(data);
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
    }
    
    public net.minidev.json.JSONArray getFinalTree(DefaultMutableTreeNode tree)
    {   
        net.minidev.json.JSONArray data2 = new net.minidev.json.JSONArray();

                                       tb = new TreeBean();
                                       tb.setId(tree.toString());
                                       tb.setText(tree.toString());
                                       tb.setClasses(tree.toString());

                                       if(tree.getChildCount()>0)
                                            tb.setHasChildren(true);
                                       else
                                            tb.setHasChildren(false);

                                       tb.setExpanded(false);

                                       data2.add(tb);
        
        return data2;
    }        
    
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
